﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Uranus.Common
{
    public class CalculateLuck
    {
        private static Random random = new Random();
        private readonly double Percent = 0.01;                    //乘以的百分点

        private List<int[]> ProbabilityMianItem = new List<int[]>();    //主概率集合
        private List<int[]> ProbabilityDepuItem = new List<int[]>();    //二次概率集合

        private readonly int MinRandom = 1;          //随机最小数
        private readonly int MaxRandom = 100000;      //随机最大数

        private int[] DefaultArr =  new int[4] { 1, 9000, 50, 60 };     //默认返回组
        private int[] DeputyLuck = new int[] { 1, 90000, 50, 100 };     //对比有二次概率计算


        public static CalculateLuck GetCalculateLuck()
        {
            return new CalculateLuck();
        }

        public CalculateLuck()
        {
            ProbabilityMianItem.Add(new int[] { 1, 98000, 50, 100 });
            ProbabilityMianItem.Add(new int[] { 98000, 98700, 100, 120 });
            ProbabilityMianItem.Add(new int[] { 98700, 99200, 120, 150 });
            ProbabilityMianItem.Add(new int[] { 99200, 99700, 150, 180 });
            ProbabilityMianItem.Add(new int[] { 99700, 99900, 180, 200 });
            ProbabilityMianItem.Add(new int[] { 99900, 100000, 200, 250 });

            ProbabilityDepuItem.Add(new int[] { 1, 50000, 50, 60 });
            ProbabilityDepuItem.Add(new int[] { 50000, 70000, 60, 70 });
            ProbabilityDepuItem.Add(new int[] { 70000, 80000, 70, 80 });
            ProbabilityDepuItem.Add(new int[] { 80000, 90000, 80, 90 });
            ProbabilityDepuItem.Add(new int[] { 90000, 100000, 90, 100 });
        }
        /// <summary>
        /// 按几率获取 收益率加成 输出参数提示语
        /// </summary>
        /// <param name="strProp">输出参数，不同的概率对应的提示语</param>
        /// <returns></returns>
        public double GetProbability(out string strProp)
        {
            double calcPercent = 0;
            int[] calcLity = this.CalculateProbability();
            if (!object.Equals(calcLity, DeputyLuck))
            {
                calcPercent = this.GetProbability(calcLity);
            }
            else
            {
                int ran = random.Next(MinRandom, MaxRandom);
                calcLity = this.CalculateProbability(ProbabilityDepuItem);
                calcPercent = this.GetProbability(calcLity);
            }
            strProp = this.GetPropContent(calcPercent);
            return calcPercent;
        }
        /// <summary>
        /// 按几率获取 收益率加成
        /// </summary>
        /// <returns></returns>
        public double GetProbability()
        {
            string strProp = string.Empty;
            return GetProbability(out strProp);
        }
        /// <summary>
        /// 根据数组获取几率
        /// </summary>
        /// <param name="paraLity"></param>
        /// <returns></returns>
        private double GetProbability(int[] paraLity)
        {
            int minCalc = paraLity[2];
            int maxCalc = paraLity[3];
            return new Random().Next(minCalc, maxCalc) * Percent;
        }

        private int[] CalculateProbability()
        {
            return this.CalculateProbability(ProbabilityMianItem);
        }

        private int[] CalculateProbability(List<int[]> eachItem)
        {
            if (eachItem == null || eachItem.Count == 0)
                return this.DefaultArr;

            //存放数组并返回
            int[] calcArr = new int[4];     

            //根据最大值最小值随机获取数字
            int calcNumber = random.Next(MinRandom, MaxRandom);   

            foreach (int[] iArr in eachItem)
            {
                int minValue = iArr[0];
                int maxValue = iArr[1];
                if (calcNumber >= minValue && calcNumber <= maxValue)
                {
                    calcArr = iArr;
                    break;
                }
            }
            return calcArr;
        }

        public string GetPropContent(double percent)
        {
            string str = string.Empty;
            if (percent > 0 && percent <= 0.8)
            {
                str = "亲，加点99#汽油再来吧！";
            }
            else if (percent > 0.8 && percent <= 1.1)
            {
                str = "要攒人品了哦！";
            }
            else if (percent > 1.1 && percent <= 1.4)
            {
                str = "苦逼的命运还能不能改变？！";
            }
            else if (percent > 1.4 && percent <= 1.7)
            {
                str = "oh，no~";
            }
            else if (percent > 1.7 && percent <= 2.0)
            {
                str = "脸是有多黑，哈哈";
            }
            else if (percent > 2.0 && percent <= 2.3)
            {
                str = "天灵灵地灵灵，财神爷爷快显灵！";
            }
            else if (percent > 2.3 && percent <= 2.6)
            {
                str = "亲，你真赞！";
            }
            else if (percent > 2.6 && percent <= 2.9)
            {
                str = "矮油，人品不错呀";
            }
            else if (percent > 2.9 && percent <= 3.2)
            {
                str = "恭喜您，距离土豪仅一步之遥！";
            }
            else if (percent > 3.2 && percent <= 3.5)
            {
                str = "太给力了，这是要逆天的节奏啊！";
            }
            return str;
        }

        /// <summary>
        /// 获取增长速度击败率
        /// </summary>
        /// <param name="price"></param>
        /// <returns></returns>
        public static int GetBeatPercent(decimal price)
        {
            if (price > 0 && price < 100)
            {
                return random.Next(1, 50);
            }
            if (price >= 100 && price < 1000)
            {
                return random.Next(50, 60);
            }
            if (price >= 1000 && price < 10000)
            {
                return random.Next(60, 70);
            }
            if (price >= 10000 && price < 100000)
            {
                return random.Next(70, 80);
            }
            if (price >= 100000)
            {
                return random.Next(80, 90);
            }
            return 10;
        }


        public static string[] ChinaReceiveRandomContent =
            {
                "中国最大质押金融服务平台",
                "我们是金融信息服务平台",
                "搭建信息分享桥梁，发布产品资讯",
                "不接触用户资金，保证资金安全",
                "不设立资金池，保证资金安全",
                "专户专款专用，保障您的资金安全",
                "第三方资金托管，为您的资金保驾护航",
                "100%本息保障，让您理财无忧",
                "利多多是一款高收益稳健型产品",
                "大同行让交易的双方不再虚假",
                "积分可以兑换增加收益的礼品卡",
                "登录页面可以找回密码",
                "提现、投资的金额大于等于1元就可",
                "提现时，持卡人与注册人身份要一致",
                "额外收益在首次回款时一次性到账哟",
                "【活动】可以了解近期活动",
                "红包、礼品卡可在【优惠券】中查看",
                "您可使用【可用余额】【包小宝】【网银支付】支付",
                "项目满标1小时后开始计息",
                "不能用他人银行卡提现",
                "提现会在一个工作日后到账",
                "充值、提现都是0手续费",
                "大同行所有操作全免手续费",
                "300积分就能兑换人品卡",
                "5000积分就能兑换屌丝卡",
                "8000积分就能兑换平民卡",
                "15000积分就能兑换土豪卡",
                "您可以在【个人中心】绑定邮箱",
                "您可以在【个人中心】更改手机号码",
                "产品只有到期后才能提现哦",
                "投资是可以获得经验值奖励",
                "投资可以获得积分奖励",
                "社区每天签到，可以获得积分和礼品卡奖励",
                "大同行APP已经上线，为您实现指尖理财",
                "我们使命就是为您真诚服务",
                "屌丝卡能带来1%额外收益",
                "平民卡能带来2%额外收益",
                "土豪卡能带来3%额外收益",
                "人品卡最多能带来3.5%额外收益",
            };

        public static string GetChinaReceiveRandom()
        {
            int calc = random.Next(0, ChinaReceiveRandomContent.Length - 1);
            return ChinaReceiveRandomContent[calc];
        }
        /// <summary>
        /// 根据概率得出是哪一个概率
        /// </summary>
        /// <param name="probabilitys">概率的数组</param>
        /// <returns>索引数</returns>
        public static int CalculateProbability(List<int> probabilitys)
        {
            int calcIndex = -1;
            if (probabilitys == null && probabilitys.Count == 0)
            {
                return calcIndex;
            }

            int sumPro = probabilitys.Sum(p => p);
            int multiple = 1000;                       //倍数
            int maxCalculate = Convert.ToInt32(sumPro * multiple);             //最大随机数
            int ran = random.Next(0, maxCalculate);
            int cate = 0;

            for (int i = 0; i < probabilitys.Count; i++)
            {
                int maxd = cate + probabilitys[i]*multiple;
                if (ran >= cate && ran < maxd)
                {
                    return i;
                }
                else
                {
                    cate = maxd;
                }
            }

            return calcIndex;
        }
    }
}
